import Vue from 'vue'
import {ACCESS_TOKEN} from '@/store/mutation-types'
// import {list as queryOrgList} from '@/api/modular/main/emsorg/emsOrgManage'
// import { VueAxios } from '@/utils/request'
import dayjs from "dayjs";

import FcImage from '@/components/FcImage/FcImage'

export let ORG_LIST = null

export let MY_BASE_URL = `/api`

export function timeFix() {
  const time = new Date()
  const hour = time.getHours()
  return hour < 9 ? '早上好' : hour <= 11 ? '上午好' : hour <= 13 ? '中午好' : hour < 20 ? '下午好' : '晚上好'
}

// 给时间后面拼上 时分秒
export function appendTime(arr) {
	var result = [];
	if(Array.isArray(arr)&&arr.length>0){
		result[0] = dayjs(arr[0]).format('YYYY-MM-DD') + ' 00:00:00';
		result[1] = dayjs(arr[1]).format('YYYY-MM-DD') + ' 23:59:59';
	}else{
		result[0] = undefined;
		result[1] = undefined;
	}
  return result;
}


// 生成年份数组，包含从2022到当前年份
export function createYearList() {
	const currentYear = new Date().getFullYear();
	const yearsArray = [];
	for (let year = 2022; year <= currentYear; year++) {
			const yearObject = {
					label: year.toString(),
					value: year.toString(),
					disable: false
			};
			yearsArray.push(yearObject);
	}
	return yearsArray;
}

export function welcome() {
  const arr = ['休息一会儿吧', '准备吃什么呢?', '要不要打一把 LOL', '我猜你可能累了']
  const index = Math.floor(Math.random() * arr.length)
  return arr[index]
}

/**
 * 触发 window.resize
 */
export function triggerWindowResizeEvent() {
  const event = document.createEvent('HTMLEvents')
  event.initEvent('resize', true, true)
  event.eventType = 'message'
  window.dispatchEvent(event)
}

export function handleScrollHeader(callback) {
  let timer = 0

  let beforeScrollTop = window.pageYOffset
  callback = callback || function () {
  }
  window.addEventListener(
    'scroll',
    event => {
      clearTimeout(timer)
      timer = setTimeout(() => {
        let direction = 'up'
        const afterScrollTop = window.pageYOffset
        const delta = afterScrollTop - beforeScrollTop
        if (delta === 0) {
          return false
        }
        direction = delta > 0 ? 'down' : 'up'
        callback(direction)
        beforeScrollTop = afterScrollTop
      }, 50)
    },
    false
  )
}

export function isIE() {
  const bw = window.navigator.userAgent
  const compare = (s) => bw.indexOf(s) >= 0
  const ie11 = (() => 'ActiveXObject' in window)()
  return compare('MSIE') || ie11
}

/**
 * Remove loading animate
 * @param id parent element id or class
 * @param timeout
 */
export function removeLoadingAnimate(id = '', timeout = 1500) {
  if (id === '') {
    return
  }
  setTimeout(() => {
    document.body.removeChild(document.getElementById(id))
  }, timeout)
}

export function getUrlParam(path) {
  let routePath = path
  let indexOfQuestionMark = routePath.indexOf('?')
  let param = routePath.substring(indexOfQuestionMark + 1)
  let params = param.split('&')
  let paramObject = {}
  for (const p of params) {
    let strings = p.split('=')
    paramObject[strings[0]] = strings.length > 1 ? strings[1] : ''
  }
  return paramObject
}

export function formatImageByColumnText(h, text) {
  let token = Vue.ls.get(ACCESS_TOKEN)
  let firstImageId = text.split(',')[0]
  // --------------修改apiTest---------------------
  let imageSrc = `/api/sysFileInfo/preview?id=${firstImageId}&token=${token}`
  // let html = `<img class="table-image" :src='${imageSrc}'>`
  let element = h('img', {
    'class': 'table-image',
    attrs: {src: imageSrc},
    style: {
      // cursor: 'pointer',
      maxWidth: '100px',
      maxHeight: '50px',
      border: 'solid 1px #cccccc',
    }
  })
  return element
}

export function createFcImage(h, imageStr) {
  let element = h(FcImage, {
    props: {
      imageIdStr: imageStr
    }
  })
  return element
}

export function formatImageUrl(text) {
  let token = Vue.ls.get(ACCESS_TOKEN)
  let firstImageId = text.split(',')[0]
    // --------------修改apiTest---------------------
  // let imageSrc = `/api/sysFileInfo/preview?id=${firstImageId}&token=${token}`
  let imageSrc = `/api/sysFileInfo/preview?id=${firstImageId}`
  return imageSrc
}

/**
 * 字典TypeId for BOM小分类
 * @type {string}
 */
export let DICT_TYPE_ID_FOR_BOM_CLASS = '1488508819728592897'

/**
 * 订单各种类
 * 1479446404789526530,1479445702801448961,入库单,in_order
 * 1479446476520513537,1479445702801448961,出库单,out_order
 * 1479447062284427265,1479445702801448961,采购单,purchase_order
 * 1479447267234897921,1479445702801448961,发货单,deliver_order
 * 1479462872516157441,1479445702801448961,生产单,produce_order
 */
export let ORDER_TYPE_ENUM = {
  inOrder: {
    id: '1479446404789526530',
    name: '入库单'
  },
  outOrder: {
    id: '1479446476520513537',
    name: '出库单'
  },
  purchaseOrder: {
    id: '1479447062284427265',
    name: '采购单'
  },
  deliverOrder: {
    id: '1479447267234897921',
    name: '发货单'
  },
  produceOrder: {
    id: '1479462872516157441',
    name: '生产单'
  },
  // 在生产单中对产品执行"完成生成"操作时,所创建的订单
  productionProductInOrder: {
    id: '1501548597948456961',
    name: '生产产品入库单'
  },
}

/**
 * 物料类型
 * 1473944069297156097,原料,material
 * 1473944318434619394,成品,product
 * 1473944389062504450,线缆,cable
 * 1473944715475824641,包材,packing
 */
export let MATERIAL_TYPE_ENUM = {
  material: {
    id: '1473944069297156097',
    name: '原料',
    code: 'material'
  },
	customized: {
    id: '1843830985384407041',
    name: '定制料',
    code: 'customized'
  },
  product: {
    id: '1473944318434619394',
    name: '成品',
    code: 'product'
  },
  cable: {
    id: '1473944389062504450',
    name: '线缆',
    code: 'cable'
  },
  packing: {
    id: '1473944715475824641',
    name: '包材',
    code: 'packing'
  },
  overseasProduct: {
    id: '1501770962390024194',
    name: '海外仓产品',
    code: 'overseas_product'
  },
}

/**
 * 订单执行状态
 * 1479467252946591746,草稿阶段,draft
 * 1479467316549017601,进行中,in_progress
 * 1479467391929049090,已完成,completed
 */
export let EXECUTE_STATUS_ENUM = {
  DRAFT: {
    id: '1479467252946591746',
    name: '草稿阶段',
    code: 'draft'
  },
  IN_PROGRESS: {
    id: '1479467316549017601',
    name: '进行中',
    code: 'in_progress'
  },
  COMPLETED: {
    id: '1479467391929049090',
    name: '已完成',
    code: 'completed'
  },
  // 这个状态是采购单专用的
  IN_STORE_COMPLETED: {
    id: '1510546430701674498',
    name: '入库已完成',
    code: 'DELIVER_IN_STORE_COMPLETED_BY_PURCHASE_ORDER'
  }
}

export let BOM_TYPE_ENUM = {
  BASE: {
    id: 1,
    name: '基础BOM'
  },
  COMBO: {
    id: 2,
    name: '组合BOM'
  }
}

export let BOM_TYPE_ENUM_ALL = {
	ALL: {
    id: '',
    name: '全部'
  },
  BASE: {
    id: 1,
    name: '基础BOM'
  },
  COMBO: {
    id: 2,
    name: '组合BOM'
  }
}

// 1440879443579334658,1440879091719172097,公司,depart_type_company,1,公司
// 1440879787235438593,1440879091719172097,供应商,depart_type_supplier,2,供应商
// 1440879943246770178,1440879091719172097,仓库,depart_type_storehouse,3,仓库
// 1440880080270487553,1440879091719172097,库位,depart_type_storehouse_position,4,库位
//
// 1440879091719172097,单位类型,depart_type,1,单位类型,0,2021-09-23 11:22:00,1265476890672672808,2021-10-04 13:06:24,1265476890672672808

export let ORG_TYPE_ENUM = {
  COMPANY: {
    id: '1440879443579334658',
    name: '公司',
    code: 'depart_type_company'
  },
  PRIMARY_SUPPLIER: {
    id: '1440879787235438593',
    name: '主要供应商',
    code: 'depart_type_primary_supplier'
  },
  NORMAL_SUPPLIER: {
    id: '1498270916314161153',
    name: '普通供应商',
    code: 'depart_type_normal_supplier'
  },
  STOREHOUSE: {
    id: '1440879943246770178',
    name: '仓库',
    code: 'depart_type_storehouse'
  },
  STOREHOUSE_POSITION: {
    id: '1440880080270487553',
    name: '库位',
    code: 'depart_type_storehouse_position'
  }
}

/**
 * 1339550467939639303,超级管理员,superAdmin,1,1,备注
 1471499885485383682,原料仓库管理员,material_admin1,6,1,原料仓库管理员
 1497105110465196033,原料供应商,Supplier,20,4,供应商
 1509475117958356994,总经理,general manager,2,1,总经理
 1509770880403845122,包材仓库管理员,material_admin2,4,1,包材仓库管理员
 1509771004215504897,线缆仓库管理员,material_admin3,3,1,线缆仓库管理员
 1509771329559277569,海外仓库管理员,material_admin5,5,1,海外仓仓库管理员
 1509784033527279617,集团子公司,gs-admin,30,4,A公司
 1510119439439482882,生产主管,sczg,12,1,生产主管
 1524594305588756482,财务,caiwu,18,1,财务
  SQCG: {id: '1549463918541459457', name: '深骐采购',},

 * @type {{STOREHOUSE: {code: string, name: string, id: string}, STOREHOUSE_POSITION: {code: string, name: string, id: string}, PRIMARY_SUPPLIER: {code: string, name: string, id: string}, NORMAL_SUPPLIER: {code: string, name: string, id: string}, COMPANY: {code: string, name: string, id: string}}}
 */
export let ROLE_ENUM = {
  SUPERADMIN: {id: '1339550467939639303', name: '超级管理员',},
  SUPPLIER: {id: '1497105110465196033', name: '原料供应商',},
  GENERALMANAGER: {id: '1509475117958356994', name: '总经理',},
  MATERIAL_ADMIN1: {id: '1471499885485383682', name: '原料仓库管理员',},
  MATERIAL_ADMIN2: {id: '1509770880403845122', name: '包材仓库管理员',},
  MATERIAL_ADMIN3: {id: '1509771004215504897', name: '线缆仓库管理员',},
  MATERIAL_ADMIN5: {id: '1509771329559277569', name: '海外仓库管理员',},
  GS_ADMIN: {id: '1509784033527279617', name: '集团子公司',},
  SCZG: {id: '1510119439439482882', name: '生产主管',},
  CAIWU: {id: '1524594305588756482', name: '财务',},
  SQCG: {id: '1549463918541459457', name: '深骐采购',},
  JSY: {id: '1820998511042441217', name: '技术员',},	
  SCWY: {id: '1820998154082004994', name: '生产文员',},	
}

export function findOrderTypeById(id) {
  for (const key in ORDER_TYPE_ENUM) {
    let item = ORDER_TYPE_ENUM[key]
    if (item.id == id) {
      return item
    }
  }
  return null
}

export function findMaterialTypeById(id) {
  if (!id) {
    return null
  }
  for (const key in MATERIAL_TYPE_ENUM) {
    let item = MATERIAL_TYPE_ENUM[key]
    if (item.id == id) {
      return item
    }
  }
  return null
}

export function findExecuteStatusById(id) {
  for (const key in EXECUTE_STATUS_ENUM) {
    let item = EXECUTE_STATUS_ENUM[key]
    if (item.id == id) {
      return item
    }
  }
  return null
}

export function setOrgList(value) {
  ORG_LIST = value
}

export function getOrgList(filterTypes) {
  if (!ORG_LIST) {
    return []
  }
  if (filterTypes && filterTypes.length > 0) {
    return ORG_LIST.filter(value => {
      return filterTypes.indexOf(value.unitRanId) > -1
    })
  }
  return ORG_LIST
}

export function findOrgById(orgId) {
  if (!ORG_LIST) {
    return null
  }
  return ORG_LIST.find(org => org.id == orgId)
}

export let EMS_ORDER_VIEW_STATE = {
  // <<< XX仓库 >>> XX表示原料\包材\线缆
  // XX仓库>待入库采购单
  waitInStoreByPurchaseOrder: 'waitInStoreByPurchaseOrder',
  // XX仓库>已入库采购单
  completedInStoreByPurchaseOrder: 'completedInStoreByPurchaseOrder',
  // XX仓库>入库单待完成
  waitInStoreByDeliverOrder: 'waitInStoreByDeliverOrder',
  // XX仓库>入库单已完成
  completedInStoreByDeliverOrder: 'completedInStoreByDeliverOrder',
  // XX仓库>生产单待出库
  waitOutStoreByProductionOrder: 'waitOutStoreByProductionOrder',
  // XX仓库>生产单已出库
  completedOutStoreByProductionOrder: 'completedOutStoreByProductionOrder',
  // XX仓库>出库单记录
  allOutStoreOrder: 'allOutStoreOrder',
  // XX仓库>无单出库单
  simpleOutStoreOrder: 'simpleOutStoreOrder',
  // XX仓库>无单入库单
  simpleInStoreOrder: 'simpleInStoreOrder',

  // <<<成品仓库>>>
  // 海外仓生产单待入库
  inStoreOfOverseasByProductionOrder: 'inStoreOfOverseasByProductionOrder',

  // <<<采购中心>>>
  // 采购中心>原料待发货采购单(等待发货的采购单)
  waitDeliverByPurchaseOrder: 'waitDeliverByPurchaseOrder',
  // 采购中心>原料进行中采购单(已经发货的采购单)
  inProgressDeliverByPurchaseOrder: 'inProgressDeliverByPurchaseOrder',
  // 采购中心>原料已完成采购单(完成发货的采购单)
  completedDeliverByPurchaseOrder: 'completedDeliverByPurchaseOrder',
  // 采购中心>发货记录
  allDeliverOrder: 'allDeliverOrder',

  // <<<生产中心>>>
  // 生产中心>生产单池
  inPoolProductionOrder: 'inPoolProductionOrder',
  // 生产中心>进行中生产单
  inProgressProductionOrder: 'inProgressProductionOrder',
  // 生产中心>已完结生产单
  completedProductionOrder: 'completedProductionOrder',
}

export let EMS_ORDER_MATERIAL_VIEW_STATE = {
  // XX仓库>待入库采购单>完成入库(非主要供应商的采购单入库操作页)
  handleInStoreByNormalSupplierPurchaseOrderMaterials: 'handleInStoreByNormalSupplierPurchaseOrderMaterials',
  // XX仓库>待入库采购单>入库记录(采购单中物料的发货\入库情况详情页) TODO 这个按钮在UI中包含了主要和普通供应商两类的采购单展示,感觉不对
  showPurchaseOrderMaterialsDetailByDeliverAndInStoreOrder: 'showPurchaseOrderMaterialsDetailByDeliverAndInStoreOrder',
  // XX仓库>待入库采购单>入库记录>明细(采购单中物料的发货\入库情况详情页)   TODO 这个按钮在UI中只显示了主要供应商采购单的发货信息,普通供应商的单呢?
  showInStoreDataByDeliverOrder: 'showInStoreDataByDeliverOrder',
  // XX仓库>已入库采购单>入库详情(采购单中物料的入库情况详情页)
  showPurchaseOrderMaterialsDetailByInStoreOrder: 'showPurchaseOrderMaterialsDetailByInStoreOrder',
  // XX仓库>入库单待完成>入库单明细(对主要供应商的发货单进行入库的操作页)
  handleInStoreByPrimarySupplierDeliverOrder: 'handleInStoreByPrimarySupplierDeliverOrder',
  // XX仓库>入库单已完成>入库单明细(对主要供应商的发货单的入库情况的查询页)
  showInStoreDataByPrimarySupplierDeliverOrder: 'showInStoreDataByPrimarySupplierDeliverOrder',
  // XX仓库>生产单待出库单>生产单出库(对进行中的生产单物料进行出库的操作页面)
  handleOutStoreByProductionOrder: 'handleOutStoreByProductionOrder',
  // XX仓库>生产单待出库单>出库详情(对进行中的生产单物料已经出库的情况进行查询的页面)
  showOutStoreDataByProductionOrder: 'showOutStoreDataByProductionOrder', // TODO 这个页面看起来有问题,为什么有个确认提交按钮
  // XX仓库>生产单已出库单>出库详情(对已经将所有物料都出库完毕的生产单的情况进行查询的页面)   !!!这个页面似乎和上面这个长的一样?
  showOutStoreDataByCompletedOutStoreProductionOrder: 'showOutStoreDataByCompletedOutStoreProductionOrder',
  // XX仓库>生产单已出库单>出库明细(展示与点击的生产单有关联的所有出库单列表)
  showOutStoreOrderListByProductionOrder: 'showOutStoreOrderListByProductionOrder',
  // XX仓库>生产单已出库单>出库明细>出库单详情(展示点击的出库单详情)  !!!与 handleOutStoreByProductionOrder 的页面很像
  showOutStoreOrderDetailByOutStoreOrder: 'showOutStoreOrderDetailByOutStoreOrder',
  // XX仓库>出库单记录>出库单明细(展示点击的出库单详情)  !!!与 showOutStoreOrderDetailByOutStoreOrder 页面是同一个
  showOutStoreOrderDetailByOutStoreOrder1: 'showOutStoreOrderDetailByOutStoreOrder1',
  // XX仓库>无单出库单>无单出库详情(展示点击的无单出库详情)
  showSimpleOutStoreOrderByOutStoreOrder: 'showSimpleOutStoreOrderByOutStoreOrder',  // !!! 无单出库详情 和 无单入库详情 很像
  // XX仓库>无单入库单>无单入库详情(展示点击的无单入库详情)
  showSimpleInStoreOrderByInStoreOrder: 'showSimpleInStoreOrderByInStoreOrder',

  // TODO  成品仓库，后面再整理

  // TODO 目前只整理到这,只把XX仓库给整理了.后面的还需要再整理

  // <<<采购中心>>>
  // 采购中心>XX(原料\线缆\包材)采购池>提交采购
  handleSubmitPurchaseOrderMaterials: 'handleSubmitPurchaseOrderMaterials',
  // 采购中心>XX(原料\线缆\包材)待发货采购单>发货明细
  showProductionMaterialsAndDeliverData: 'showProductionMaterialsAndDeliverData',
  // 采购中心>XX(原料\线缆\包材)待发货采购单>发货明细>发货明细
  showDeliverDataByProductionMaterial: 'showDeliverDataByProductionMaterial',
  // 采购中心>XX(原料\线缆\包材)待发货采购单>发货
  handleSubmitDeliverOrderFormByPurchaseOrder: 'handleSubmitDeliverOrderFormByPurchaseOrder',
  // 采购中心>XX(原料\线缆\包材)发货记录>发货详情 TODO 这个展示的是列表订单，其中包含了聚合的物料，这个目录得再考虑一下
  showDeliverOrderDetailByDeliverOrder: 'showDeliverOrderDetailByDeliverOrder',

  // <<<生产中心>>>
  // 生产中心>生产单池>产品列表
  showProductListByInDraftProductionOrder: 'showProductListByInDraftProductionOrder',
  // 生产中心>生产单池>生产单>新增产品
  handleSubmitProductFormByInDraftProductionOrder: 'handleSubmitProductFormByInDraftProductionOrder',
  // 生产中心>进行中生产单>产品列表
  showProductsByInProgressProductionOrder: 'showProductsByInProgressProductionOrder',
  // 生产中心>进行中生产单>产品列表>产品明细
  showProductDetailByProductionOrderProducts: 'showProductDetailByProductionOrderProducts',
  // 生产中心>进行中生产单>包材出库
  showPackingOutStoreDataByProductionOrder: 'showPackingOutStoreDataByProductionOrder',
  // 生产中心>进行中生产单>包材出库>出库记录
  showPackingOutStoreDetailByOutStoreOrder: 'showPackingOutStoreDetailByOutStoreOrder',

}

export default {
  formatImageByColumnText,
  formatImageUrl,
  createFcImage,
  getUrlParam,
  findOrderTypeById,
  findMaterialTypeById,
  findExecuteStatusById,
  BOM_TYPE_ENUM,
	BOM_TYPE_ENUM_ALL,
  EMS_ORDER_VIEW_STATE,
  EMS_ORDER_MATERIAL_VIEW_STATE,
  ORG_LIST,
  ROLE_ENUM,
  setOrgList,
  getOrgList,
  findOrgById,
}
